<template>
  <div class="commonActivityPage">
    <div class="acTab">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="全部" name="1" />
        <el-tab-pane label="我参与的" name="2" />
      </el-tabs>
      <el-radio-group v-model="searchForm.signType" @change="signTypeChange">
        <el-radio-button :label="1">平台优惠券</el-radio-button>
        <el-radio-button :label="2">平台秒杀</el-radio-button>
        <el-radio-button :label="3">平台限时折扣</el-radio-button>
      </el-radio-group>
    </div>
    <!-- 搜索 -->
    <FixedNav class="formSearch" :height="260">
      <div class="searchItem">
        <span>活动名称：</span>
        <el-input
          v-model="searchForm.activityName"
          maxlength="15"
          placeholder="请输入活动名称"
        />
      </div>
      <div class="searchItem">
        <span>活动状态：</span>
        <el-select
          v-model="searchForm.state"
          placeholder="请选择活动状态"
          clearable
        >
          <el-option label="报名未开始" value="0" />
          <el-option label="报名进行中" value="1" />
          <el-option label="活动待开始" value="2" />
          <el-option label="活动进行中" value="3" />
          <el-option label="活动已结束" value="4" />
        </el-select>
      </div>
      <div class="searchItem">
        <span>审核状态：</span>
        <el-select
          v-model="searchForm.examineState"
          placeholder="请选择审核状态"
          clearable
        >
          <el-option label="待审核" value="0" />
          <el-option label="报名成功" value="1" />
          <el-option label="报名失败" value="2" />
        </el-select>
      </div>
      <el-button type="primary" plain @click="search">查询</el-button>
      <el-button plain @click="clear">重置</el-button>
    </FixedNav>
    <!-- 表格 -->
    <div class="tableBox">
      <el-table
        v-loading="tableLoading"
        :data="tableData"
        border
        :header-cell-style="{ background: '#EEF3FF', color: '#333333' }"
        tooltip-effect="dark"
        style="width: 100%"
        class="dataTable"
      >
        <el-table-column prop="activityName" label="活动名称" show-overflow-tooltip />
        <el-table-column prop="bondMoney" label="活动保证金" show-overflow-tooltip />
        <el-table-column prop="signTime" label="报名时间" width="170" />
        <el-table-column prop="activityTime" label="活动时间" width="170" />
        <el-table-column label="活动优惠" show-overflow-tooltip>
          <template #default="scope">
            <div v-if="scope.row.discountProgramme == 1">满减</div>
            <div v-if="scope.row.discountProgramme == 2">优惠券</div>
            <div>{{ scope.row.details }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="activityIntroduce" label="活动简介" show-overflow-tooltip />
        <el-table-column label="活动状态" show-overflow-tooltip>
          <template #default="scope">
            <span v-if="scope.row.state == 0">报名未开始</span>
            <span v-if="scope.row.state == 1">报名进行中</span>
            <span v-if="scope.row.state == 2">活动待开始</span>
            <span v-if="scope.row.state == 3">活动进行中</span>
            <span v-if="scope.row.state == 4">活动已结束</span>
          </template>
        </el-table-column>
        <el-table-column label="报名状态" show-overflow-tooltip>
          <template #default="scope">
            <span v-if="scope.row.signState == 0">待审核</span>
            <span v-if="scope.row.signState == 1">报名成功</span>
            <span v-if="scope.row.signState == 2">报名失败</span>
            <span v-if="scope.row.signState == null">未报名</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" show-overflow-tooltip width="220">
          <template #default="scope">
            <div class="btnList">
              <el-button v-if="scope.row.operation === 1" type="primary" @click="getTotal(scope.row, 1)">立即报名</el-button>
              <el-button v-if="scope.row.operation === 2" type="primary" disabled @click="getTotal(scope.row, 1)">立即报名</el-button>
              <el-button v-if="scope.row.operation === 4" type="primary" @click="getTotal(scope.row, 1)">重新报名</el-button>
              <el-button v-if="scope.row.operation === 5" type="primary" @click="getTotal(scope.row, 1)">继续报名</el-button>
              <el-button v-if="scope.row.operation === 3" @click="goActiveData(scope.row)">活动数据</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="fenye">
        <el-pagination
          v-model:current-page="searchForm.page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="searchForm.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          background
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <el-dialog
      v-model="signVisible"
      title="活动报名"
      width="80%"
      center
      :close-on-click-modal="false"
      @close="closeDialog"
    >
      <SignActivity ref="signActivityRef" :form="form" @cancel="goList" />
    </el-dialog>

    <el-dialog
      v-model="detailVisible"
      title="活动报名"
      width="80%"
      center
      :close-on-click-modal="false"
    >
      <DataActivity ref="activityDataRef" :form="form" @cancel="detailVisible = false" />
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onBeforeMount } from 'vue'
import FixedNav from '@/components/FixedNav/index.vue'
import SignActivity from '@/views/active/getActive.vue'
import DataActivity from '@/views/active/activeData.vue'
import { activityGetAll } from '@/api/active.js'

const activeName = ref('1')
const searchForm = ref({
  type: 1, // 查询类型 1-全部 2-我参与的
  activityName: '', // 活动名称
  state: '',
  examineState: '',
  page: 1,
  pageSize: 10,
  signType: 1
})
const total = ref(1)
const tableLoading = ref(false)
const tableData = ref([])
const signVisible = ref(false)
const signActivityRef = ref(null)
const activityDataRef = ref(null)
const form = ref({})
const detailVisible = ref(false)
const active = ref(1)

onBeforeMount(() => {
  getAll()
})
const closeDialog = () => {
  signActivityRef.value.reset()
}
const goList = () => {
  signVisible.value = false
  getAll()
}
const signTypeChange = () => {
  getAll()
}
const handleSizeChange = (val) => {
  searchForm.value.pageSize = val
  getAll()
}
const handleCurrentChange = (val) => {
  searchForm.value.page = val
  getAll()
}
// 活动切换
const handleClick = (tab, event) => {
  console.log(tab.paneName)
  searchForm.value.type = parseInt(tab.paneName)
  getAll()
}
// 查询
const search = () => {
  total.value = 1
  searchForm.value.page = 1
  getAll()
}
// 清除
const clear = () => {
  activeName.value = '1'
  searchForm.value = {
    type: 1,
    signType: 1,
    activityName: '',
    state: '',
    examineState: '',
    page: 1,
    pageSize: 10
  }
  getAll()
}
// 立即报名
const getTotal = (row, index) => {
  active.value = 1
  signVisible.value = true
  form.value = {
    activityId: row.activityId,
    bondMoney: row.bondMoney,
    type: index,
    ifBond: row.ifBond,
    signType: row.signType
  }
}
// 活动数据
const goActiveData = (row) => {
  form.value = {
    activityName: row.activityName,
    activityId: row.activityId,
    signId: row.signId,
  }
  detailVisible.value = true
}
// 初始化查询所有数据
const getAll = async () => {
  try {
    tableLoading.value = true
    const res = await activityGetAll(searchForm.value)
    tableData.value = res.data.list
    total.value = res.data.total
  } finally {
    tableLoading.value = false
  }
}
</script>

<style lang="scss" scoped>
.commonActivityPage {
  padding: 20px;
  margin-top: 20px;
  background-color: #FFFFFF;
  :deep(.acTab) {
    .el-radio-button__inner {
      height: 38px;
      line-height: 1.5;
    }
  }
  .formSearch {
    padding: 20px 0;
    display: flex;
    .searchItem {
      margin-right: 20px;
      display: flex;
      align-items: center;
      // line-height: 32px;
      span {
        min-width: 80px;
      }
    }
  }
}
</style>
